<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="73px">
      <el-form-item label="编号" prop="code" >
        <el-input
          v-model="queryParams.code"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 240px;margin-right: 40px"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 240px;margin-right: 40px"
        />
      </el-form-item>
      <el-form-item label="简称" prop="shortName">
        <el-input
          v-model="queryParams.shortName"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 240px;margin-right: 30px"
        />
      </el-form-item>
      <br/>
      <el-form-item label="SKU编号" prop="skuCode">
        <el-input
          v-model="queryParams.skuCode"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 240px;margin-right: 40px"
        />
      </el-form-item>
      <el-form-item label="类目" prop="categoryId" >
        <el-select v-model="queryParams.categoryId" @keyup.enter.native="handleQuery" clearable placeholder="" style="width: 240px;margin-right: 40px">
          <el-option v-for="cate in Category"
                     :key="cate.id"
                     :value="cate.id"
                     :label="cate.name">{{cate.name}}</el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="品牌" prop="brandId" >
          <el-select v-model="queryParams.brandId" @keyup.enter.native="handleQuery" clearable placeholder="" style="width: 240px;margin-right: 40px">
            <el-option v-for="brand in Brand"
                       :key="brand.id"
                       :value="brand.id"
                       :label="brand.name">{{brand.name}}</el-option>
          </el-select>
        </el-form-item>
      <br/>
      <el-form-item label="商品类型" prop="productType">
        <el-select v-model="queryParams.productType" @keyup.enter.native="handleQuery" clearable placeholder="全部"  style="width: 240px;margin-right: 40px">
          <el-option value="1" label="普通商品">普通商品</el-option>
          <el-option value="2" label="组合商品">组合商品</el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px;margin-right: 40px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          ></el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="available">
        <el-select v-model="queryParams.available" @keyup.enter.native="handleQuery" clearable placeholder="全部"  style="width: 240px;margin-right: 40px">
          <el-option value="1" label="启用">启用</el-option>
          <el-option value="0" label="停用">停用</el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:ProductInfo:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ProductInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="ID" align="center" prop="id" />-->
      <el-table-column label="编号" align="center" prop="code" show-overflow-tooltip width="200px"/>
      <el-table-column label="图片" align="center" prop="imageUrl">
        <template slot-scope="scope">
          <img :src="scope.row.imageUrl" style="width: 50px;height: 50px" />
        </template>
      </el-table-column>
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="简称" align="center" prop="shortName" />
      <el-table-column label="SKU" align="center" prop="skuCode" show-overflow-tooltip width="200px"/>
      <el-table-column label="类目" align="center" prop="categoryName"/>
      <el-table-column label="品牌" align="center" prop="brandName" />
      <el-table-column label="商品类型" align="center" prop="productType">
        <template slot-scope="scope">
          <span>{{ scope.row.productType == 1 ? "普通商品" : "组合商品" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="available">
        <template slot-scope="scope">
          <span>{{ scope.row.available == 1 ? "启用" : "停用" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleShow(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:ProductInfo:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <!-- 查看商品详情 -->
    <el-dialog title="查看" :visible.sync="open1" width="1200px" append-to-body>
        <el-descriptions :column="2" border :data="haseShow">
          <el-descriptions-item label="编号">{{haseShow.code}}</el-descriptions-item>
          <el-descriptions-item label="名称">{{haseShow.name}}</el-descriptions-item>
          <el-descriptions-item label="简称">{{haseShow.shortName}}</el-descriptions-item>
          <el-descriptions-item label="SKU编号">{{haseShow.skuCode}}</el-descriptions-item>
          <el-descriptions-item label="外部编号">{{haseShow.externalCode}}</el-descriptions-item>
          <el-descriptions-item label="类目">{{haseShow.categoryName}}</el-descriptions-item>
          <el-descriptions-item label="品牌">{{haseShow.brandName}}</el-descriptions-item>
          <el-descriptions-item label="规格">{{haseShow.spec}}</el-descriptions-item>
          <el-descriptions-item label="单位">{{haseShow.unit}}</el-descriptions-item>
          <el-descriptions-item label="重量(kg)">{{haseShow.weight}}</el-descriptions-item>
          <el-descriptions-item label="体积">{{haseShow.volume}}</el-descriptions-item>
          <el-descriptions-item label="采购价(元)">{{haseShow.purchasePrice}}</el-descriptions-item>
          <el-descriptions-item label="销售价(元)">{{haseShow.salePrice}}</el-descriptions-item>
          <el-descriptions-item label="零售价(元)">{{haseShow.retailPrice}}</el-descriptions-item>
          <el-descriptions-item :span="2" label="商品类型">{{haseShow.productType == 1 ? "普通商品" : "组合商品"}}</el-descriptions-item>
          <el-descriptions-item :span="2" label="状态">{{haseShow.available == 1 ? "启用" : "停用"}}</el-descriptions-item>
          <el-descriptions-item :span="2" :label="pro.propertyName" v-for="pro in haseShow.properties">{{pro.propertyText}}</el-descriptions-item>
          <el-descriptions-item :span="2" label="图片">
            <template>
              <img :src="haseShow.imageUrl" style="width: 100px;height: 100px" />
            </template>
          </el-descriptions-item>
        </el-descriptions>

    </el-dialog>


    <!-- 添加或修改商品对话框 -->
    <el-dialog  :title="title"  :visible.sync="open" width="1100px" append-to-body>
      <el-form ref="form" :inline="true"  :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商品类型" prop="productType" v-show="showtype">
          <el-select v-model="form.productType" @change="hashShow" style="width: 200px">
            <el-option :value="1" label="普通商品">普通商品</el-option>
            <el-option :value="2" label="组合商品">组合商品</el-option>
          </el-select>
        </el-form-item>
        <div v-show="this.formShow">
<!--        <el-form-item label="编号" prop="code">-->
<!--          <el-input v-model="form.code" placeholder="请输入编号" style="width: 200px"/>-->
<!--        </el-form-item>-->
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="简称" prop="shortName">
          <el-input v-model="form.shortName" placeholder="请输入简称" style="width: 200px"/>
        </el-form-item>
<!--        <el-form-item label="SKU编号" prop="skuCode">-->
<!--          <el-input v-model="form.skuCode" placeholder="请输入SKU" style="width: 200px"/>-->
<!--        </el-form-item>-->
        <el-form-item label="外部编号" prop="externalCode">
          <el-input v-model="form.externalCode" placeholder="请输入外部编号" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="类目" prop="categoryId" >
          <el-select v-model="form.categoryId" clearable style="width: 200px">
            <el-option v-for="cate in Category"
                       :key="cate.id"
                       :value="cate.id"
                       :label="cate.name">{{cate.name}}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌" prop="brandId" >
          <el-select v-model="form.brandId" clearable style="width: 200px">
            <el-option v-for="brand in Brand"
                       :key="brand.id"
                       :value="brand.id"
                       :label="brand.name">{{brand.name}}</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格" prop="spec">
          <el-input v-model="form.spec" placeholder="请输入规格" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="重量" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入重量" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="体积" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入体积" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="采购价(元)" prop="purchasePrice" v-if="form.productType == 1">
          <el-input v-model="form.purchasePrice" placeholder="请输入采购价" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="销售价(元)" prop="salePrice">
          <el-input v-model="form.salePrice" placeholder="请输入销售价" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="零售价(元)" prop="retailPrice">
          <el-input v-model="form.retailPrice" placeholder="请输入零售价" style="width: 200px"/>
        </el-form-item>
        <el-form-item label="状态" prop="available" v-if="form.id != null">
          <el-select v-model="form.available" style="width: 200px">
            <el-option :value="1" label="启用">启用</el-option>
            <el-option :value="0" label="停用">停用</el-option>
          </el-select>
        </el-form-item>
          <br/>
<!--        <el-form-item label="图片路径" prop="imageUrl" v-if="form.id==null">-->
<!--          <image-upload v-model="form.imageUrl"/>-->
<!--        </el-form-item>-->

        <el-form-item  label="图片路径" prop="imageUrl" v-if="form.id==null">
          <el-upload
            class="avatar-uploader"
            :action="uploadUrl"
            :show-file-list="false"
            :on-success="handleUploadSuccess"
            :before-upload="beforeUpload">
            <img v-if="form.imageUrl" width="100px" :src="form.imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div slot="tip" class="el-upload__tip"></div>
          </el-upload>
        </el-form-item>


        </div>
      </el-form>


      <div v-if="this.form.productType==2">
        <el-divider content-position="center">组合商品信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddBaseDataProductBundle">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteBaseDataProductBundle">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="baseDataProductBundleList" :row-class-name="rowBaseDataProductBundleIndex" @selection-change="handleBaseDataProductBundleSelectionChange" ref="baseDataProductBundle">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="单品" prop="productId" width="760">
            <template slot-scope="scope">
              <el-select v-model="scope.row.productId" clearable style="width: 100%">
                <el-option v-for="list in ProductInfoList1"
                           :key="list.id"
                           :value="list.id"
                           :label="list.name">{{list.name}}</el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="包含数量" prop="bundleNum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.bundleNum" placeholder="请输入包含数量" />
            </template>
          </el-table-column>
          <el-table-column label="销售价" prop="salePrice" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.salePrice" placeholder="请输入销售价" />
            </template>
          </el-table-column>
          <el-table-column label="零售价" prop="retailPrice" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.retailPrice" placeholder="请输入零售价" />
            </template>
          </el-table-column>
        </el-table>
      </div>


      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { listProductInfo, getProductInfo, delProductInfo, addProductInfo, updateProductInfo } from "@/api/system/ProductInfo";
//引入类目
import {listProductCategory} from "@/api/system/ProductCategory"
//引入品牌
import {listProductBrand} from "@/api/system/ProductBrand"

export default {
  name: "ProductInfo",
  data() {
    return {
      uploadUrl: process.env.VUE_APP_BASE_API + "/file/upload",
      codeNum:null,
      skucodeNum:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品表格数据
      ProductInfoList: [],
      // 组合商品表格数据
      ProductInfoList1: [],
      // 采购收货单明细表格数据
      baseDataProductBundleList: [],
      Category:[],
      Brand:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1:false,
      showtype:false,
      formShow:false,
      // 备注时间范围
      daterangeCreateTime: [],
      haseShow:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        shortName: null,
        skuCode: null,
        categoryId: null,
        brandId: null,
        productType: null,
        available: null,
        createTime: null,
      },
      queryParams1: {
        pageNum: 1,
        pageSize: 100000,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        skuCode: [
          { required: true, message: "SKU不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "类目不能为空", trigger: "blur" }
        ],
        brandId: [
          { required: true, message: "品牌不能为空", trigger: "blur" }
        ],
        productType: [
          { required: true, message: "商品类型不能为空", trigger: "change" }
        ],
        available: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        createById: [
          { required: true, message: "创建人ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" }
        ],
        updateById: [
          { required: true, message: "修改人ID不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" }
        ],
        purchasePrice: [
          { required: true, message: "采购价不能为空", trigger: "blur" }
        ],
        salePrice: [
          { required: true, message: "销售价不能为空", trigger: "blur" }
        ],
        retailPrice: [
          { required: true, message: "零售价不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getList1();
    this.getCategoryList();
    this.getBrandList();
  },
  methods: {

    // 图片上传前校验
    beforeUpload(file) {
      const isImage = ['image/jpeg', 'image/png'].includes(file.type);
      const isLt5M = file.size / 1024 / 1024 < 5;

      if (!isImage) {
        this.$message.error('只能上传 JPG/PNG 格式的图片!');
      }
      if (!isLt5M) {
        this.$message.error('图片大小不能超过 5MB!');
      }

      return isImage && isLt5M;
    },

    // 上传成功处理
    handleUploadSuccess(response, file) {
      if (response.code === 200) {
        this.form.imageUrl = response.data;
        // this.$message.success('图片上传成功');
      } else {
        this.$message.error('图片上传失败: ' + response.msg);
      }
    },

    //当商品类型发生变化时，显示表单，隐藏商品类型选项
    hashShow(){
      this.formShow = true;
      this.showtype =false;
    },
    /** 查询商品列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listProductInfo(this.queryParams).then(response => {
        this.ProductInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询商品列表 */
    getList1() {
      this.loading = true;
      listProductInfo(this.queryParams1).then(response => {
        this.ProductInfoList1 = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 类目
    getCategoryList() {
      this.loading = true;
      listProductCategory(this.queryParams1).then(response => {
        this.Category = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 品牌
    getBrandList(){
      this.loading = true;
      listProductBrand(this.queryParams1).then(response => {
        this.Brand = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        shortName: null,
        skuCode: null,
        externalCode: null,
        categoryId: null,
        brandId: null,
        productType: null,
        spec: null,
        unit: null,
        weight: null,
        volume: null,
        available: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateById: null,
        updateTime: null,
        imageUrl: null
      };
      this.baseDataProductBundleList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.showtype=true;  //显示商品类型选项
      this.formShow=false;  //隐藏表单
      this.reset();
      // this.$router.push({
      //   path:'/ProductInfo/add',
      // })
      this.open = true;
      this.title = "添加商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.formShow=true;  //显示表单
      this.showtype=false;  //隐藏商品类型选项
      this.reset();
      const id = row.id || this.ids
      getProductInfo(id).then(response => {
        this.form = response.data;
        this.baseDataProductBundleList = response.data.baseDataProductBundleList;
        this.codeNum = this.form.code;
        this.skucodeNum = this.form.skuCode;
        this.open = true;
        this.title = "修改商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid && this.formInfo()) {
          this.form.baseDataProductBundleList = this.baseDataProductBundleList;
          if (this.form.id != null) {
            updateProductInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const Code = getOrderCode(this.ProductInfoList);
            this.form.code = Code;
            const Sku = getOrderSku(this.ProductInfoList);
            this.form.skuCode = Sku;
            addProductInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    formInfo(){
      if (this.form.productType == 2){
        if (this.baseDataProductBundleList.length == 0){
          this.$modal.msgError("组合商品必须包含商品单品！");
          return false;
        }
        const arrt = this.baseDataProductBundleList; // 组合商品不允许重复
        for (let i = 0; i < arrt.length; i++){
          if(arrt[i].productId ==null || arrt[i].productId ==""){
            this.$modal.msgError("单品不允许为空");
            return false;
          }
          if(arrt[i].bundleNum ==null || arrt[i].bundleNum ==""){
            this.$modal.msgError("包含数量不允许为空");
            return false;
          }
          if(arrt[i].salePrice ==null || arrt[i].salePrice ==""){
            this.$modal.msgError("销售价不允许为空");
            return false;
          }
          if(arrt[i].retailPrice ==null || arrt[i].retailPrice ==""){
            this.$modal.msgError("零售价不允许为空");
            return false;
          }
        }

        const arr = this.baseDataProductBundleList; // 组合商品不允许重复
        for (let i = 0; i < arr.length; i++) {
          for (let j = i + 1; j < arr.length; j++) {
            if (arr[i].productId == arr[j].productId) {
              this.$modal.msgError("第"+ (i + 1) +"行项目与其他行重复，请检查！");
              return false; // 发现重复
            }
          }
        }

        var sumSale = 0;
        var sumRetail = 0;
        for (let i=0;i<arr.length;i++){
          sumSale+=arr[i].bundleNum*arr[i].salePrice;
          sumRetail+=arr[i].bundleNum*arr[i].retailPrice;
        }
        if (sumSale!=this.form.salePrice){
          this.$modal.msgError("当前所有单品的【包含数量】乘以【销售价（元）】的总和为"+this.form.salePrice+"元，组合商品的销售价为"+sumSale+"元，两个值不相等，请调整！");
          return false;
        }
        if (sumRetail!=this.form.retailPrice){
          this.$modal.msgError("当前所有单品的【包含数量】乘以【零售价（元）】的总和为"+this.form.retailPrice+"元，组合商品的零售价为"+sumRetail+"元，两个值不相等，请调整！");
          return false;
        }
      }
      return  true;
    },
    /** 查看商品详情*/
    handleShow(row){
      this.loading = true;
      const id = row.id || this.ids
      getProductInfo(id).then(response => {
        this.haseShow = response.data;
        this.open1 = true;
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品编号为"' + ids + '"的数据项？').then(function() {
        return delProductInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    /** 组合商品序号 */
    rowBaseDataProductBundleIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 组合商品添加按钮操作 */
    handleAddBaseDataProductBundle() {
      let obj = {};
      obj.productId = "";
      obj.bundleNum = "";
      obj.salePrice = "";
      obj.retailPrice = "";
      this.baseDataProductBundleList.push(obj);
    },
    /** 组合商品删除按钮操作 */
    handleDeleteBaseDataProductBundle() {
      if (this.checkedBaseDataProductBundle.length == 0) {
        this.$modal.msgError("请先选择要删除的组合商品数据");
      } else {
        const baseDataProductBundleList = this.baseDataProductBundleList;
        const checkedBaseDataProductBundle = this.checkedBaseDataProductBundle;
        this.baseDataProductBundleList = baseDataProductBundleList.filter(function(item) {
          return checkedBaseDataProductBundle.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleBaseDataProductBundleSelectionChange(selection) {
      this.checkedBaseDataProductBundle = selection.map(item => item.index)
    },
  },
};

//单号生成
function getOrderCode(list) {
  var date = new Date(),
    year = date.getFullYear(),
    month = date.getMonth() + 1, // 月份是从0开始的
    day = date.getDate();

// 如果月份或日期是个位数，在前面补0
  month = month < 10 ? '0' + month : month;
  day = day < 10 ? '0' + day : day;

  var time=year + '' + month + '' + day

  let type="SPGL"; //商品管理
  var code=time+'0000000'
  var str1=code.slice(0,7)
  var str2=code.slice(7,)

  if (str2.slice(0,1)=="0"){
    str1=code.slice(0,6)
    str2=code.slice(6,)

    for (var i=0;i<list.length;i++){
      let listCode=list[i].code.slice(4,);
      if (listCode.slice(0, 8)===time){
        if (parseInt(listCode.slice(6,))>parseInt(str2)){
          code=listCode;
          str2=code.slice(6,)
        }
      }
    }
  }else {
    for (var i=0;i<list.length;i++){
      let listCode=list[i].code.slice(4,);
      if (listCode.slice(0, 8)===time){
        if (parseInt(listCode.slice(7,))>parseInt(str2)){
          code=listCode;
          str2=code.slice(7,)
        }
      }
    }
  }

  code=type+str1+(parseInt(str2)+1)

  return code.toString();
}


//单号生成
function getOrderSku(list) {
  var date = new Date(),
    year = date.getFullYear(),
    month = date.getMonth() + 1, // 月份是从0开始的
    day = date.getDate();

// 如果月份或日期是个位数，在前面补0
  month = month < 10 ? '0' + month : month;
  day = day < 10 ? '0' + day : day;

  var time=year + '' + month + '' + day

  let type="SPSK"; //商品SKU
  var code=time+'9865248'
  var str1=code.slice(0,7)
  var str2=code.slice(7,)

  if (str2.slice(0,1)=="0"){
    str1=code.slice(0,6)
    str2=code.slice(6,)

    for (var i=0;i<list.length;i++){
      let listCode=list[i].skuCode.slice(4,);
      if (listCode.slice(0, 8)===time){
        if (parseInt(listCode.slice(6,))>parseInt(str2)){
          code=listCode;
          str2=code.slice(6,)
        }
      }
    }
  }else {
    for (var i=0;i<list.length;i++){
      let listCode=list[i].skuCode.slice(4,);
      if (listCode.slice(0, 8)===time){
        if (parseInt(listCode.slice(7,))>parseInt(str2)){
          code=listCode;
          str2=code.slice(7,)
        }
      }
    }
  }

  code=type+str1+(parseInt(str2)+1)

  return code.toString();
}
</script>

